package com.yusysqdy.gulimail.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yusysqdy.common.valid.AddGroup;
import com.yusysqdy.common.valid.UpdateGroup;
import com.yusysqdy.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.yusysqdy.gulimail.product.entity.BrandEntity;
import com.yusysqdy.gulimail.product.service.BrandService;
import com.yusysqdy.common.utils.PageUtils;
import com.yusysqdy.common.utils.R;


/**
 * 品牌
 *
 * @author qindongyang
 * @email qindy0116@126.com
 * @date 2020-12-10 11:23:39
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        return R.ok().put("brand", brand);
    }

    @GetMapping("/infos")
    public R info(@RequestParam("brandIds") List<Long> brandIds) {
        List<BrandEntity> brand = brandService.getBrandByIds(brandIds);
        return R.ok().put("data", brand);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*BindingResult bindingResult*/) {
       /* if (bindingResult.hasErrors()) {
            Map<String, String> mapError = new HashMap<>();
            // 获取校验的错误结果
            bindingResult.getFieldErrors().forEach((item) -> {
                // FieldError 获取到错误提示消息
                String message = item.getDefaultMessage();
                // 获取错误的属性名字
                String field = item.getField();
                mapError.put(field, message);
                System.out.println("嘿嘿嘿");
            });

            return R.error(400, "提交数据不合法").put("data", mapError);
        } else {

        }*/
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
